<template>
    <div class="app-container">

        <SearchContent :queryParams="queryParams" @query="handleQuery" @reset="resetQuery">
            <el-col :span="6">
                <el-form-item label="诉讼编号:" prop="proceedCode">
                    <el-input v-model="queryParams.proceedCode" clearable placeholder="请输入诉讼编号"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="6">
                <el-form-item label="客户名称:" prop="customName">
                    <el-input v-model="queryParams.customName" clearable placeholder="请输入客户名称"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="6">
                <el-form-item label="完成时间:" prop="sendTime">
                    <el-date-picker type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format='yyyy-MM-dd'
                        style="width: 100%;" v-model="timeRange"  @change="changeTime">
                    </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="6">
                <el-form-item label="诉讼状态:" prop="proceedStatus">
                    <el-select v-model="queryParams.proceedStatus" clearable>
                        <el-option :key="item.value" :label="item.label" :value="item.value"
                            v-for="item in progressLitigation"></el-option>
                    </el-select>
                </el-form-item>
            </el-col>

        </SearchContent>

        <div class="card-wrap paddingAll20" :model="queryParams" label-width="86px">
            <!-- <div class="table-title-wrap justify-end">
                <el-button type="primary" size="mini" @click="handleOperate('',7)"
                           v-hasPermi="['purchaseManage:purchaseList:add']" :disabled="completingLitigationStatus">完成诉讼
                </el-button>
            </div> -->
            <table-page :loading="loading" :dataList="dataList" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @setSizeCurrent="getList" 
						@selectionChange="selectionChange" border>
                <!-- 操作插槽 -->
                <template slot="tableColAc">
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" max-width="180px">
                        <template v-slot="{ row }">
                            <e-button size="mini" type="text" @click="handleOperate(row,1)"
                                v-hasPermi="['collectionManagement:litigationHandling:see']">查看
                            </e-button>
                            <e-button size="mini" type="text" @click="handleOperate(row,2)" v-if="row.status !==3"
                                v-hasPermi="['collectionManagement:litigationHandling:programme']">提交方案
                            </e-button>
                            <e-button size="mini" type="text" @click="handleOperate(row,3)" v-if="row.status==1"
                                v-hasPermi="['collectionManagement:litigationHandling:compromise']">提交和解结果
                            </e-button>
                            <e-button size="mini" type="text" @click="handleOperate(row,4)" v-if="row.status==2"
                                v-hasPermi="['collectionManagement:litigationHandling:mediate']">提交调解结果
                            </e-button>
                            <e-button size="mini" type="text" @click="handleOperate(row,5)"  v-if="row.status !==3"
                                v-hasPermi="['collectionManagement:litigationHandling:judgment']">提交判决结果
                            </e-button>
                            <e-button size="mini" type="text" @click="handleOperate(row,6)"  v-if="row.status !==3"
                                v-hasPermi="['collectionManagement:litigationHandling:force']">提交强制执行
                            </e-button>
                            <el-button  type="text" size="mini" @click="handleOperate(row,7)" v-if="row.proceedStatus==4"
                                v-hasPermi="['purchaseManage:purchaseList:add']">完成诉讼
                            </el-button>
                        </template>
                    </el-table-column>
                </template>
                <!-- 普通列插槽 -->
                <template slot="tableCol">
                    <el-table-column label="诉讼编号" align="center" prop="proceedCode" min-width="120px"
                        show-overflow-tooltip >
                        <template v-slot="{ row }">
                            <span class="goOther" @click="litigationGo(row)">{{ row.proceedCode }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="订单编号" align="center" prop="orderCode" min-width="120px"
                        show-overflow-tooltip >
                        <template v-slot="{ row }">
                            <span class="goOther" @click="orderGo(row)">{{ row.orderCode }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="客户名称" align="center" prop="customName" min-width="120px"
                        show-overflow-tooltip />
                    <el-table-column label="诉讼状态" align="center" prop="proceedStatus" min-width="120px"
                        show-overflow-tooltip>
                        <template v-slot="{ row }">
                            <dict-tag :options="dict.type.progress_litigation" :value="row.proceedStatus" />
                        </template>
                    </el-table-column>
                    <el-table-column label="完成时间" align="center" prop="completeTime" min-width="120px"
                        show-overflow-tooltip />
                </template>
            </table-page>

        </div>
        
        <!-- 提交方案 -->
        <el-dialog :title="title" :visible.sync="open" append-to-body class="form-dialog" width="45%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="160px" class="demo-ruleForm">
                <el-form-item label="诉讼方案类型" prop="resource" v-if="dialogType==2">
                    <el-radio-group v-model="ruleForm.resource">
                    <el-radio :label=1>和解</el-radio>
                    <el-radio :label=2>调解</el-radio>
                    </el-radio-group>
                </el-form-item>
               
                <el-form-item label="和解协议" prop="notice" v-if="dialogType==3">
                    <ImageCommon  v-model="ruleForm.notice"  :up-type="2" info="选择和解协议" :limit="20"></ImageCommon>
                </el-form-item>
                <el-form-item label="和解金额" prop="amount" v-if="dialogType==3">
                    <el-input  v-model="ruleForm.amount" placeholder="选输和解解金额" v-thousand></el-input>
                </el-form-item>
                <el-form-item label="调解书" prop="notice" v-if="dialogType==4">
                    <ImageCommon  v-model="ruleForm.notice"  :up-type="2" info="选择调解书" :limit="20"></ImageCommon>
                </el-form-item>
                <el-form-item label="调解金额" prop="amount" v-if="dialogType==4">
                    <el-input  v-model="ruleForm.amount" placeholder="选输入调解金额" v-thousand></el-input>
                </el-form-item>
                <el-form-item label="判决书" prop="notice" v-if="dialogType==5">
                    <ImageCommon  v-model="ruleForm.notice"  :up-type="2" info="选择判决书" :limit="20"></ImageCommon>
                </el-form-item>
                <el-form-item label="判决金额" prop="amount" v-if="dialogType==5">
                    <el-input  v-model="ruleForm.amount" placeholder="选输入判决金额" v-thousand></el-input>
                </el-form-item>
                <el-form-item label="强制执行立案通知书" prop="notice" v-if="dialogType==6">
                    <ImageCommon  v-model="ruleForm.notice"  :up-type="2" info="选择强制执行立案通知书" :limit="20"></ImageCommon>
                </el-form-item>
                <el-form-item label="最后一笔支付时间" prop="endPayTime" v-if="dialogType==3 || dialogType==4 || dialogType==5">
                    <el-date-picker type="date" placeholder="选择最后一笔支付时间" value-format='yyyy-MM-dd'
                       style="width:100%" v-model="ruleForm.endPayTime">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="完成时间" prop="completeTime" v-if="dialogType==7">
                    <el-date-picker type="date" placeholder="选择完成时间" value-format='yyyy-MM-dd'
                        style="width:100%" v-model="ruleForm.completeTime">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="说明" prop="explainText">
                    <el-input type="textarea" max="2000" v-model="ruleForm.explainText"></el-input>
                </el-form-item>
                <el-form-item label="附件" prop="file">
                    <ImageCommon  v-model="ruleForm.file"  :up-type="2" info="选择附件" :limit="20"></ImageCommon>
                </el-form-item>
                
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
                <el-button @click="resetForm('ruleForm')">取消</el-button>
            </div> 
        </el-dialog>
    </div>
</template>

<script>
import SearchContent from "@/components/SearchContent/index.vue";
import * as apis from "@/api/collectionManagement/litigationApplication.js";
import { OrderParam,} from '@/api/finance/manage/collect/api.js';
export default {
    name: "LitigationHandling",
    components: {
        SearchContent,
    },
    dicts: [ 'message_type','progress_litigation'],
    data() {
        var amoutRule = (rule, value, callback) => {
            console.log(value,'value')
            let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
            if (!msg.test(value)) {
                this.ruleForm.amount = "";
                callback(new Error("请输入数字"));
            } else {
                this.ruleForm.amount = value.toString().match(/^\d+(?:\.\d{0,2})?/)[0];
                callback();
            }
        };
        return {
            // 遮罩层
            loading: false,
            retutnCarTimes: [],
            progressLitigation:[],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                proceedCode: null,
                customName: null,
                proceedStatus: null,
                startTime:'',
                endTime:'',
                completeTime:'',
            },
            total: 0,
            dataList: [],
            title: '查看',
            form: {
                type: '',
                paramName: '',
            },
            open: false,
            busFlowTypeList: [
                {
                    dictLabel:'诉讼中',
                    dictSort:1
                },
                {
                    dictLabel:'诉讼完成',
                    dictSort:2
                },
            ],
            ruleForm:{
                completeTime:1,
                completeExplain:'',
                file:'',
                proceedHandleId:'',
                explainText:'',
                amount:'',
                endPayTime:'',
                notice:'',
            },
            rules:{
                resource:[
                    { required: true, message: '请选择方案类型', trigger: 'blur' },
                ],
                notice:[
                    { required: true, message: '请选择相关附件', trigger: 'blur' },
                ],
                explainText:[
                    { required: true, message: '请输入方案说明', trigger: 'blur' },
                ],
                amount:[
                    { required: true, message: '请输入金额', trigger: 'blur',validator:amoutRule, },
                ],
                completeTime:[
                    { required: true, message: '请选择完成时间', trigger: 'blur' },
                ]
            },
            title:'',
            dialogType:'',
            timeRange : [],
            completingLitigationStatus:true,
            isSelect:true,
            info:{},
            type:null,
            ids:'',
        };
    },
    async created() { 
        let list = await this._getDicts("progress_litigation");
        list.map((res)=>{
            if(res.dictLabel=='诉讼中' || res.dictLabel=='诉讼完成'){
                let obj = {
                    value:'',
                    label:'',
                }
                obj.value = res.dictValue
                obj.label = res.dictLabel
                this.progressLitigation.push(obj)
            }
           
        })
        
    },

    mounted() {
        this.getList()
    },
    // async created() {
    //     this.busFlowTypeList = await this._getDicts("message_type");
    // },
    activated() {
        this.handleQuery()
    },
    methods: {
        // 诉讼编号跳转
        litigationGo(info){
            this.$router.push({name: 'IitigationApplicationinfo',query:{id:info.proceedMainId}})
        },
        // 订单编号跳转
        orderGo(info){
            OrderParam(info.orderCode).then((res)=>{
                if(res.code==200){
                    this.$router.push({
                            name: 'OrderDetails',
                            query: {
                            type: res.data.carType,
                            beFrom: 'OrderView',
                            orderId: res.data.orderId,
                            preOrderId: res.data.preOrderId,
                            calcSchemeId: res.data.calcSchemeId,
                            },
                        })
                }
            })
            
        },
        // 勾选中的数据
        selectionChange(val){
            console.log(val)
            if(val.length>0){
                this.completingLitigationStatus = false
                let list = []
                val.map((res)=>{
                    list.push(res.id)
                })
                this.ids = list.join(',')
            }else{
                this.completingLitigationStatus = true
            }
        },
        // 创建时间发生改变
        changeTime(val) {
            this.queryParams.startTime = val[0]
            this.queryParams.endTime = val[1]
        },
        //提交方案确定
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                if(this.title== '完成诉讼'){
                    let obj  ={
                        // id:this.ids,
                        id:this.info?.id,
                        completeTime:this.ruleForm?.completeTime,
                        completeExplain:this.ruleForm?.explainText,
                        file:this.ruleForm?.file,
                        proceedMainId:this.info?.proceedMainId
                    }
                    apis.putHandleDetails(obj).then((res)=>{
                        if(res.code==200){
                            this.$message.success('提交成功')
                            this.open = false
                            this.getList()
                        }
                    })  
                }else{
                    let handleType = null
                    switch (this.type){ 
                        case 2:
                        // '提交方案'
                            if(this.ruleForm?.resource==1){
                                handleType = 0
                            }else if(this.ruleForm?.resource==2){
                                handleType = 2
                            }
                            
                        break
                        case 3:
                        // '提交和解结果'
                            handleType = 1
                        break
                        case 4:
                            // '提交调解结果'
                            handleType = 3
                        break
                        case 5:
                        // '提交判决结果'
                            handleType = 4
                        break
                        case 6:
                            // '提交强制执行'
                            handleType = 5
                        break
                      
                    }
                    let obj={
                        proceedHandleId:this.info?.id,
                        handleType:handleType,
                        explainText:this.ruleForm.explainText,
                        file:this.ruleForm.file,
                        amount:this.ruleForm.amount,
                        endPayTime:this.ruleForm.endPayTime,
                        notice:this.ruleForm.notice,
                    }
                    apis.postHandleDetails(obj).then((res)=>{
                        if(res.code==200){
                            this.$message.success('提交成功')
                            this.open = false
                            this.getList()
                        }
                    })  
                }
                  
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        // 提交方案取消
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.open = false
            
        },
        /** 查询列表 */
        getList() {
            this.loading = true;
            apis.proceedHandle(this.queryParams).then(res => {
                this.dataList = res.rows;
                this.total = res.total;
            }).finally(() => {
                this.loading = false
            })
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },

        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                proceedCode: null,
                customName: null,
                proceedStatus: null,
            },
            this.timeRange = []
            this.handleQuery();
        },


        handleOperate(row,type) {
            this.info = row
            this.type = type
            this.dialogType = type
            this.ruleForm={
                completeTime:1,
                completeExplain:'',
                file:'',
                proceedHandleId:'',
                explainText:'',
                amount:'',
                endPayTime:'',
                notice:'',
                completeTime:'',
            },
            console.log(type,111111)
            switch (type){
                case 1:
                    this.$router.push({name:'IitigationHandlinginfo',query:{id:row.id}})
                break
                case 2:
                    this.open = true
                    this.title= '提交方案'
                break
                case 3:
                    this.open = true
                    this.title= '提交和解结果'
                break
                case 4:
                    this.open = true
                    this.title= '提交调解结果'
                break
                case 5:
                    this.open = true
                    this.title= '提交判决结果'
                break
                case 6:
                    this.open = true
                    this.title= '提交强制执行'
                break
                case 7:
                    this.open = true
                    this.title= '完成诉讼'
                break
            }
           
        },
    }
};
</script>
<style scoped>
.goOther{
    color: blueviolet;
    cursor: pointer;
}
</style>
